﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;

namespace Com.CMS263.DAO
{
    public class ProdGoodsAttrDAO : BaseService
    {


        /// <summary>
        /// 
        /// </summary>
        /// <param name="artId"></param>
        /// <param name="attrId"></param>
        /// <returns></returns>
        public string GetValueById(int goodsId, int attrId)
        {
            return DBHelper.ExecuteHQL("select AttrValue from  ProdGoodsAttr where GoodsId=" + goodsId + " and AttrId=" + attrId + "").UniqueResult<string>();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="artId"></param>
        /// <param name="attrId"></param>
        /// <returns></returns>
        public ProdGoodsAttr GetExtValueById(int goodsId, int attrId)
        {
            try
            {
                return DBHelper.ExecuteHQL("from  ProdGoodsAttr where GoodsId=" + goodsId + " and AttrId=" + attrId).List<ProdGoodsAttr>().First();
            }
            catch (Exception)
            {
                return null;
            }
        }

        #region 根据ProdTypeId查询个数
        /// <summary>
        /// 根据ProdTypeId查询个数
        /// </summary>
        /// <param name="ptid"></param>
        /// <returns></returns>
        public int GetCountByProdTypeId(int ptid)
        {
            string hql = "select count(*) from ProdGoodsAttr where AttrId in ( select id from ProdAttribute where ProdTypeId=" + ptid + ")";
            return Convert.ToInt32(GetValue(hql));
        }
        #endregion

        #region 根据Prodtypeid删除相关信
        /// <summary>
        /// 根据Prodtypeid删除相关信
        /// </summary>
        /// <param name="ptid"></param>
        /// <returns></returns>
        public int DelByProdTypeId(int ptid)
        {
            string hql = "delete from ProdGoodsAttr where AttrId in ( select id from ProdAttribute where ProdTypeId=" + ptid + ")";
            return DBHelper.ExecuteHQL(hql).ExecuteUpdate();
        }
        #endregion


        public IList<ProdGoodsAttr> FindListByGoodsId(int id)
        {
            string hql = "from ProdGoodsAttr where GoodsId=" + id;
            return FindList<ProdGoodsAttr>(hql, null, null, null);
        }

        #region 根据AttrId查询个数
        /// <summary>
        /// 根据AttrId查询个数
        /// </summary>
        /// <param name="attrid"></param>
        /// <returns></returns>
        public int GetCountByAttrId(int attrid)
        {
            string hql = "select count(*) from ProdGoodsAttr where AttrId=" + attrid;
            return Convert.ToInt32(GetValue(hql));
        }
        #endregion

        #region 根据AttrId删除相关信息
        /// <summary>
        /// 根据AttrId删除相关信息
        /// </summary>
        /// <param name="attrid"></param>
        /// <returns></returns>
        public int DelByAttrId(int attrid)
        {
            string hql = "delete from ProdGoodsAttr where AttrId=" + attrid;
            return DBHelper.ExecuteHQL(hql).ExecuteUpdate();
        }
        #endregion


        public void DelBygooId(int goodId)
        {
            string hql = "delete from ProdGoodsAttr where GoodsId="+goodId;
            DBHelper.ExecuteHQL(hql).ExecuteUpdate();
        }
    }
}
